#include "http.h"

Http::Http(QString url,                                     //需要自己构造?后的字符串
           Http::Method method)                             //只支持异步
    :method(method)
{

    manager = new QNetworkAccessManager();
    request = QNetworkRequest((QUrl(url)));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
}

Http::~Http()
{
    delete manager;
}

void Http::setData(QString &data)
{
    Http::data = data.toUtf8();
}

void Http::setData(QByteArray data)
{
    Http::data = data;
}

void Http::setRequest(QNetworkRequest request)
{
    Http::request = request;
}

QNetworkRequest Http::getRequest()
{
    return request;
}

QNetworkAccessManager *Http::getAccessManager()
{
    return manager;
}

void Http::send()
{
    switch (method) {
    case Http::GET: manager->get(request); break;
    case Http::HEAD: manager->head(request); break;
    case Http::POST: manager->post(request, data); break;
    case Http::PUT: manager->put(request, data); break;
    case Http::DELETE: manager->deleteResource(request); break;
    default:
        break;
    }
}
